package com.jk.controller;


import com.jk.bo.EsDestinationBO;
import com.jk.po.DestinationPO;
import com.jk.po.SharingPO;
import com.jk.service.EsService;
import com.jk.vo.CustomerVO;
import com.jk.vo.EsDestinationVO;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 段卓帆
 * @date 2021/6/30 11:23
 * @return es
 */
@RestController
@RequestMapping("es")
@Api("es操作接口")
public class EsController {
    //日志记录对象
    private static Logger logger = LoggerFactory.getLogger(TestController.class);
    @Autowired
    private EsService esService;
    /**
    * @Author  郑滨
    * @method  queryDestination
    * @description 查询景区跟地点表数据 结合Es使用
    * @Date 11:10 2021/7/7
    * @Param  * @param
    * @return java.util.List<com.jk.po.DestinationPO>
    **/
    @PostMapping("queryDestination")
    @ApiOperation("查询目的地跟景区数据")
    public @ResponseBody
    List<EsDestinationVO> queryDestination(){
        return esService.queryDestination();
    }
    /**
    * @Author  郑滨
    * @method  queryDestinationES
    * @description 从es中查询路线信息并作条查
    * @Date 9:59 2021/7/8
    * @Param  * @param search
    * @return java.util.List<com.jk.bo.EsDestinationBO>
    **/
    @PostMapping(value = "queryDestinationES")
    @ApiOperation(value = "从es中查询路线信息并作条查")
    public List<EsDestinationBO> queryDestinationES(@RequestBody EsDestinationBO search){
        return esService.queryDestinationES(search);
    }

    @PostMapping("queryEsMoBan")
    @ApiOperation("Es条件查询模板生成")
    public void queryEsMoBan(@RequestBody EsDestinationBO search) throws IOException {
        //获取路径
        File file = new File("");
        String path =file.getCanonicalPath();
        //字符串拼接路径
        String filePath=path+"/"+"lvyou-consumer"+"/"+"src/"+"main/"+"resources/"+"static/"+"nav"+"/"+"esplace"+"/";
        //进入条查页面
        List<EsDestinationBO> list = esService.queryDestinationES(search);

        FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
        freeMarkerConfigurationFactoryBean.setTemplateLoaderPath("classpath:templates");
        Configuration configuration = null;
        FileWriter fileWriter = null;

        try {
            try {
                configuration = freeMarkerConfigurationFactoryBean.createConfiguration();
                Template template = configuration.getTemplate("es.ftl");
                fileWriter = new FileWriter(filePath+"es"+search.getAname()+".html");

                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("list",list);

                template.process(map,fileWriter);
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if ( fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
